package cn.gson.springboot.controllers;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.springboot.model.pojos.ClinicCard;
import cn.gson.springboot.model.pojos.DrugApply;
import cn.gson.springboot.model.service.DrugApplyService;
import cn.gson.springboot.model.service.DrugService;
import cn.gson.springboot.model.service.DrugstorageService;
import cn.gson.springboot.model.service.InhospitalstorageService;
import cn.gson.springboot.model.service.OutpatientstorageService;
import cn.gson.springboot.model.vo.DrugfyVo;
import cn.gson.springboot.model.vo.DrugqlVo;

@Controller
public class PharmacyController {
	
	//查询请领记录
	@Autowired
	private DrugApplyService ql;
	
	//查询药品信息
	@Autowired
	private DrugService ds;
	
	//查询门诊库存药品信息
	@Autowired
	private OutpatientstorageService mzkc;
	
	//查询住院库存药品信息
	@Autowired
	private InhospitalstorageService zykc;
	
	//查看门诊药品库存
	@Autowired
	private  OutpatientstorageService outpatientstorageservice; //药品
	
	//查看门诊药品库存
	@Autowired
	private  InhospitalstorageService inhospitalstorageservice; //药品
	
	/*1*/
		//药房发药界面
		/*@RequestMapping("z-dispensingshelves")
		public String fayao(Model model) {
			//发药表随机数
			int hospid=(int)((Math.random()*4+1)*100000);
			model.addAttribute("hospid", hospid);
			return "z-dispensingshelves";
		}*/
		
		/*//药品信息的新增
		@PostMapping(value="add-Drugyaozx")
		@ResponseBody             
		public String addDrugyaozx(String healthno_fa,Integer publicdurg_id,Integer publicdurgshuliang,Integer publicdurgshijishuliang,
				Integer publicdurgtype,BigDecimal publicdurgallmoney,String publicdurgpeope,String publicdurglingyaopeople) {
		try {
			outpatientstorageservice.addDrugyaozx(healthno_fa, publicdurg_id, publicdurgshuliang, publicdurgshijishuliang, publicdurgtype, publicdurgallmoney, publicdurgpeope, publicdurglingyaopeople);
			 return "发药成功";
		}catch (Exception e) {
			e.printStackTrace();
			return "发药失败";
		}
		
	}*/
		
		//显示住院发药信息
		@PostMapping("findDrugzyqljl")
		@ResponseBody
		public Map<String,Object>  findDrugzyqljl(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			//进行分页
			 Page<Object> pg = PageHelper.startPage(page, rows);
			 JSONArray list = outpatientstorageservice.selectAllDrugmzfy();
			
			//进行数据格式转换和封装
			Map<String,Object> map = new HashMap<>();
			map.put("total", pg.getTotal());//封装总记录数
			System.out.println("1:"+pg.getTotal());
			map.put("rows", list);//封装当前页的内容
			System.out.println("2:"+ list.size());
			return map;
		}
		
		
		
		//住院新增发药记录
		@PostMapping(value="save-Bydrugfyjl")
		@ResponseBody
		public String saveqlByfaydrug(DrugfyVo drugfyVo) {
			try {
				outpatientstorageservice.DrugIndent(drugfyVo);
				return "请领成功";
			} catch (Exception e) {
				e.printStackTrace();
				return "请领失败";
			}
		}
		
		/*//门诊发药记录
		@PostMapping(value="add-mzfyjl")
		@ResponseBody
		public void addmzfyjl(String drugmzid,String drugshulmz,Integer publicdurgshuliang,Integer publicdurgshijishuliang,
				Integer publicdurgtype,BigDecimal publicdurgallmoney,String publicdurgpeope,String publicdurglingyaopeople) {
				if(!("").equals(drugmzid)) {
					String arrs []=drugmzid.split(",");
						for (int i = 0; i < arrs.length; i++) {
							System.out.println("遍历获取到的药品id："+arrs[i]);
		
						}				
				}
				String arrs2 []=drugshulmz.split(",");
				for (int i = 0; i < arrs2.length; i++) {
					System.out.println("遍历获取到的药品数量："+arrs2[i]);

				}
		}
		*/
		//显示门诊医疗卡
				@PostMapping("finddrugallmzylk")
				@ResponseBody
				public Map<String,Object>  findDrugmzylk(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows); 
					 JSONArray list = outpatientstorageservice.selectAllmzylkxs();
					
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}
				
				//显示住院医疗卡
				@PostMapping("finddrugallzyylk")
				@ResponseBody
				public Map<String,Object>  findDrugzyylk(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows);
					 JSONArray list = outpatientstorageservice.selectAllzyylkxs();
					
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}
				
				//显示住院医疗卡详情
				@PostMapping("selectAllzyjlxq")
				@ResponseBody
				public Map<String,Object>  selectAllzyjlxq(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows,Integer pid){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows);
					 JSONArray list = outpatientstorageservice.selectAllzyylkxq(pid);
					
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}
				
			/*	//门诊医疗卡详情
				@PostMapping("findall-indentmzylk")
				@ResponseBody
				public  String  Findallinmzylk(Integer cliniccardid){
						JSONObject  json = new JSONObject();
						JSONArray selectAllmzylk = outpatientstorageservice.selectAllmzylk2(cliniccardid);
						json.put("result", selectAllmzylk);
						return json.toJSONString();
				}		
*/

				
				//搜索门诊医疗卡信息
				@PostMapping("findallmzylkxx-drug")
				@ResponseBody
				public  Map<String,Object>  Findallmzylkxx(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows, Integer cliniccardid){

						//进行分页
						Page<Object> pg = PageHelper.startPage(page, rows);
						JSONArray selectAllmzylk = outpatientstorageservice.selectAllmzylk(cliniccardid);
						for (Object o : selectAllmzylk) {
							System.out.println(o);
						}
						//进行数据格式转换和封装
						Map<String,Object> map = new HashMap<>();
						map.put("total", pg.getTotal());//封装总记录数
						map.put("rows", selectAllmzylk);//封装当前页的内容
			
						return map;
				}	
				
				//搜索住院医疗卡信息
				@PostMapping("FindAllzyylk-drug")
				@ResponseBody
				public  Map<String,Object>  FindAllzyylk(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows, Integer cliniccardid){

						//进行分页
						Page<Object> pg = PageHelper.startPage(page, rows);
						JSONArray selectAllmzylk = outpatientstorageservice.selectAllzyylk(cliniccardid);
						for (Object o : selectAllmzylk) {
							System.out.println(o);
						}
						//进行数据格式转换和封装
						Map<String,Object> map = new HashMap<>();
						map.put("total", pg.getTotal());//封装总记录数
						map.put("rows", selectAllmzylk);//封装当前页的内容
			
						return map;
				}
				
				//搜索住院记录信息
				@PostMapping("selectAllDrugmzfycx")
				@ResponseBody
				public  Map<String,Object>  selectAllDrugmzfycx(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows, Integer publicdurgid){

						//进行分页
						Page<Object> pg = PageHelper.startPage(page, rows);
						JSONArray selectAllmzylk = outpatientstorageservice.selectAllDrugzyfycx(publicdurgid);
						for (Object o : selectAllmzylk) {
							System.out.println(o);
						}
						//进行数据格式转换和封装
						Map<String,Object> map = new HashMap<>();
						map.put("total", pg.getTotal());//封装总记录数
						map.put("rows", selectAllmzylk);//封装当前页的内容
			
						return map;
				}
				
				
				//显示住院医疗卡详情药品信息
				@RequestMapping("finddrugallzyxq")
				@ResponseBody
				public Map<String,Object>  findDrug2(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows,Integer cid){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows);
					 JSONArray list = outpatientstorageservice.selectAllzyylkxq(cid);
					for (Object object : list) {
						System.out.println(object);
					}
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}
				
				//显示门诊医疗卡详情药品信息
				@RequestMapping("finddrugallmzxq")
				@ResponseBody
				public Map<String,Object>  finddrugallmzxq(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows,Integer cid){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows);
					 JSONArray list = outpatientstorageservice.selectAllmzylkxq(cid);
					for (Object object : list) {
						System.out.println(object);
					}
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}
				
				
				
				
				
			/*	//药房库存界面
				@RequestMapping("z-pharmacyinventory")  
				public void yaokucun() {
					
				}*/
				//搜索请领单
				@PostMapping("Findallqlcx-drugcx")
				@ResponseBody
				public  Map<String,Object>  Findallqlcx(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows, Integer drugapplyid){

						//进行分页
						Page<Object> pg = PageHelper.startPage(page, rows);
						JSONArray selectAllqlidkaid = ql.selectAllqlidkaid(drugapplyid);
						for (Object o : selectAllqlidkaid) {
							System.out.println(o);
						}
						//进行数据格式转换和封装
						Map<String,Object> map = new HashMap<>();
						map.put("total", pg.getTotal());//封装总记录数
						map.put("rows", selectAllqlidkaid);//封装当前页的内容 
						return map;
				}	
				
				
				
				//显示请领记录的详情信息
				@RequestMapping("findAllqlxq")
				@ResponseBody
				public Map<String,Object>  findAllqlxq(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows,Integer drugid){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows);
					 JSONArray list = outpatientstorageservice.selectAllqlxq(drugid);
					for (Object object : list) {
						System.out.println(object);
					}
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}
				
	
		       
			/*ql.addDrugql(drugapplyallnumber,drugapplytype,
					drugapplypeople, drugapplystate);*/
	
		
		
		/*//根据请领记录查询
		@RequestMapping("z-pharmacyinventory")  
		public void qlcx(Integer drugapplyid) {
		List<DrugApply> qljl = ql.selectAllbycardid(drugapplyid);  
		}*/
				
				//显示请领记录
				@RequestMapping("add-xinzengjilu")
				@ResponseBody
				public Map<String,Object>  findDrugql(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows);
					 JSONArray list = ql.selectAllqljl();
					
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}	
		
				//新增请领记录
				@PostMapping("save-Bydrugqljl")
				@ResponseBody
				public String saveqlBydrug(DrugqlVo drugqlVo) {
					try {
						ql.DrugIndent(drugqlVo);
						return "请领成功";
					} catch (Exception e) {
						e.printStackTrace();
						return "请领失败";
					}
				}
		
		
		//查询门诊所有药品信息
				@PostMapping("findallyf-drug")
				@ResponseBody
				public  Map<String,Object>  Findalldrug(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows, String drugname){

						//进行分页
						Page<Object> pg = PageHelper.startPage(page, rows);
						JSONArray selectAllmzkc = outpatientstorageservice.mzkcmapper(drugname);
						for (Object o : selectAllmzkc) {
							System.out.println(o);
						}
						//进行数据格式转换和封装
						Map<String,Object> map = new HashMap<>();
						map.put("total", pg.getTotal());//封装总记录数
						map.put("rows", selectAllmzkc);//封装当前页的内容
			
						return map;
				}
				
				//查询住院所有药品
				@PostMapping("findallzy-drug")
				@ResponseBody
				public  Map<String,Object>  Findallzydrug(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows, String drugname){

						//进行分页
						Page<Object> pg = PageHelper.startPage(page, rows);
						JSONArray selectAllzykc = inhospitalstorageservice.zykcmapper(drugname);
						for (Object o : selectAllzykc) {
							System.out.println(o);
						}
						//进行数据格式转换和封装
						Map<String,Object> map = new HashMap<>();
						map.put("total", pg.getTotal());//封装总记录数
						map.put("rows", selectAllzykc);//封装当前页的内容
			
						return map;
				}
				
				//查询药品信息
				@RequestMapping("finddrugall")
				@ResponseBody
				public Map<String,Object>  findDrug(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows);
					 JSONArray list = ds.selectAllDrug();
					
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}

				//药品药库的新增
				@PostMapping(value="add-Drugyaokuxz")
				@ResponseBody             
				public String addDrugyaokuxz(String healthno_ru,Integer drugnumber,Integer outpatientpici,
						Integer outpatientstorageamount) {
				try {
					outpatientstorageservice.addDrugyaokuxz(healthno_ru, drugnumber, outpatientpici, outpatientstorageamount);
					 return "入库成功";
				}catch (Exception e) {
					e.printStackTrace();
					return "入库失败";
				}
				
			}
		/*//药房库存管理界面
		@RequestMapping("z-pharmacyingunli") 
		public void kucunguanli() {
					
		}*/
		
		//查询门诊药品信息
		@RequestMapping("finddrugmzkc")
		@ResponseBody
		public Map<String,Object>  findoutpatientstorage(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			//进行分页
			 Page<Object> pg = PageHelper.startPage(page, rows);
			 JSONArray list = mzkc.selectAllzmck() ;
			
			//进行数据格式转换和封装
			Map<String,Object> map = new HashMap<>();
			map.put("total", pg.getTotal());//封装总记录数
			System.out.println("1:"+pg.getTotal());
			map.put("rows", list);//封装当前页的内容
			System.out.println("2:"+ list.size());
			return map;
		}
		
		//查询住院药品信息
				@RequestMapping("finddrugzykc")
				@ResponseBody
				public Map<String,Object>  findinhospitalstorage(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows);
					 JSONArray list = zykc.selectAllzykc();
					
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}		


		
		/*//盘点界面
		@RequestMapping("z-check") 
		public void pandian() {
					
		}*/
		
		//显示盘点药品信息
				@RequestMapping("findpandian-Drug")
				@ResponseBody
				public Map<String,Object>  findpandianDrug(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows);
					 JSONArray list = outpatientstorageservice.selectAllpandianxx();
					
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}
				
				//显示新增盘点药品信息
				@RequestMapping("findpandianxz-Drug")
				@ResponseBody
				public Map<String,Object>  findpandianxzDrug(
						@RequestParam("page")int page,
						@RequestParam("rows")int rows){
					//进行分页
					 Page<Object> pg = PageHelper.startPage(page, rows);
					 JSONArray list = outpatientstorageservice.selectAllpandianxzxx();
					
					//进行数据格式转换和封装
					Map<String,Object> map = new HashMap<>();
					map.put("total", pg.getTotal());//封装总记录数
					System.out.println("1:"+pg.getTotal());
					map.put("rows", list);//封装当前页的内容
					System.out.println("2:"+ list.size());
					return map;
				}

		
		/*//退药界面
		@RequestMapping("z-drugrepercussion") 
		public void tuiyao() {
							
		}*/
		
}
